//
//  ChangeNameviewController.m
//  project
//
//  Created by 张振宇 on 15/11/5.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "ChangeNameviewController.h"
#import <AFNetworking.h>
@interface ChangeNameviewController ()
@property (nonatomic, strong) UITextField *nameTF;
@end

@implementation ChangeNameviewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    _nameTF = [[UITextField alloc]initWithFrame:CGRectMake(0,80 , kWidth, 40)];
    _nameTF.backgroundColor = [UIColor whiteColor];
    _nameTF.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"nickname"];
    [self.view addSubview:_nameTF];
        self.view.backgroundColor = [[UIColor alloc]initWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(30, 150, kWidth - 60, 30);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(didChangeName) forControlEvents:UIControlEventAllEvents];
    [self.view addSubview:btn];
    
    
    // Do any additional setup after loading the view.
}

- (void)didChangeName
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:_nameTF.text forKey:@"nickname"];
    [[AFHTTPSessionManager manager] PUT:@"http://api.liwushuo.com/v2/users/me" parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        [[NSUserDefaults standardUserDefaults]setValue:_nameTF.text forKey:@"nickname"];
        NSLog(@"%@",responseObject);
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
